<?php
/**
 * FeedbackController is used for management of feedback of merchants.
 * 
 * LBB Solutions Oy 版权所有 © 2013
 */
class FeedbackController extends Controller
{
	/**
	 * @return array action filters
	 */
	public function filters() {
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}
	
	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules() {
		return array(
			array('allow', // allow authenticated user to perform below actions
				'users'=>array('@'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}
	
	/**
	 * Get feedback list
	 */
	public function actionIndex() {
		$this->render('index');
	}
	
	/**
	 * Get feedback form
	 */
	public function actionForm() {
		$model = new FeedbackForm();
		
		if (isset($_POST['ajax']) && $_POST['ajax'] === 'feedback-form') {
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
		
		if (isset($_POST['FeedbackForm'])) {
			$model->attributes = $_POST['FeedbackForm'];
			$model->id_merchant = Yii::app()->user->id;
			if ($model->save()) {				
				//send email to business@yamii.fi
				$subject = 'Feedback from ' . Yii::app()->user->getState('name');
				BUtils::SendEmailIn($subject, $model->email, $model->content);
				
				Yii::app()->user->setFlash('success', BUtils::T('feedback_success_msg'));
				$this->refresh();
			}
		}
		$this->render('form', array('model'=>$model));
	}
}